package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.aop.TimeMeasure;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/23 12:36
 */
@TimeMeasure(granular = GranularType.MIL_SECOND)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao ;
    @Override
    public User login(String username, String password) {
        User user = new User() ;
        if(Objects.equals(user.getName(), "123")) {

        }
        return userDao.selectOne(username, password) ;
    }

    @Override
    public List<User> findUserByName(String username) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectByName(username) ;
    }
    @Override
    public User queryByName(String username){
        User user = new User() ;
        if(Objects.equals(user.getName(), username)) {
            return user ;
        }else {
        return null;
    }}
    @Override
    public int register(User user) {
        if (user == null || user.getName() == null || user.getPassword() == null) {
            return 0; // 注册失败，用户信息无效
        }
       int result= userDao.insert(user);
        return result > 0 ? 1 : 0;
    }
    @Override
    public PageResult queryUsers(User userCondition, Integer pageNum, Integer pageSize) {

        int totalRecords = userDao.selectCountAll();

        PageResult pageResult = new PageResult();
                 // 设置用户列表
        pageResult.setTotal(totalRecords); // 设置总用户数
        pageResult.setPageNum(pageNum);       // 设置当前页码
        pageResult.setPageSize(pageSize);     // 设置每页的记录数

         return pageResult;
    }
    @Override
    public void addAssignRole(Integer userId, List<Integer> roleIds) {
        if (userId == null || roleIds == null || roleIds.isEmpty()) {
            throw new IllegalArgumentException("用户ID或角色ID不能为空");
        }
for (Integer roleId : roleIds)
       userDao.insertUserRole(userId,roleId);

    }


}
